package org.starapp.rbac.action;

import java.util.Date;
import java.util.Map;
import javax.servlet.ServletRequest;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.starapp.core.web.Servlets;
import com.google.common.collect.Maps;
import org.starapp.rbac.entity.Objects;
import org.starapp.rbac.service.ObjectsService;

/**
 * Objects管理的Controller, 使用Restful风格的Urls:
 * 

@RequestMapping(value = "/admin/objects")
 * List page     : GET /admin/objects/
 * Create page   : GET /admin/objects/create
 * Create action : POST /admin/objects/create
 * Update page   : GET /admin/objects/update/{id}
 * Update action : POST /admin/objects/update
 * Delete action : GET /admin/objects/delete/{id}
 * 
 * @author wangqiang@cisoft.com.cn
 */
@Controller
@RequestMapping(value = "/admin/objects")
public class ObjectsController {

	private static final int PAGE_SIZE = 3;

	private static Map<String, String> sortTypes = Maps.newLinkedHashMap();
	static {
		sortTypes.put("auto", "自动");
		sortTypes.put("title", "标题");
	}

	@Autowired
	private ObjectsService objectsService;

	@RequestMapping(value = "")
	public String list(@RequestParam(value = "sortType", defaultValue = "auto") String sortType,
			@RequestParam(value = "page", defaultValue = "1") int pageNumber, Model model, ServletRequest request) {
		Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, "search_");

		Page<Objects> objects = objectsService.getObjectsList( searchParams, pageNumber, PAGE_SIZE, sortType);

		model.addAttribute("objects", objects);
		model.addAttribute("sortType", sortType);
		model.addAttribute("sortTypes", sortTypes);
		// 将搜索条件编码成字符串，用于排序，分页的URL
		model.addAttribute("searchParams", Servlets.encodeParameterStringWithPrefix(searchParams, "search_"));

		return "admin/objects/objectsList";
	}

	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String createForm(Model model) {
		model.addAttribute("objects", new Objects());
		model.addAttribute("action", "create");
		return "admin/objects/objectsForm";
	}

	@RequestMapping(value = "create", method = RequestMethod.POST)
	public String create(@Valid Objects entity, RedirectAttributes redirectAttributes) {
		objectsService.saveObjects(entity);
		redirectAttributes.addFlashAttribute("message", "create success");
		
		return "redirect:/admin/objects/";
	}

	@RequestMapping(value = "update/{objectId}", method = RequestMethod.GET)
	public String updateForm(@PathVariable("objectId") String objectId, Model model) {
		model.addAttribute("objects", objectsService.getObjects(objectId));
		model.addAttribute("action", "update");
		return "admin/objects/objectsForm";
	}

	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String update(@Valid @ModelAttribute("preloadTask") Objects entity, RedirectAttributes redirectAttributes) {
		objectsService.saveObjects(entity);
		redirectAttributes.addFlashAttribute("message", "update success");
		return "redirect:/admin/objects/";
		
	}

	@RequestMapping(value = "delete/{objectId}")
	public String delete(@PathVariable("objectId") String objectId, RedirectAttributes redirectAttributes) {
		objectsService.deleteObjects(objectId);
		redirectAttributes.addFlashAttribute("message", "delete success");
		return "redirect:/admin/objects/";
		
	}

	/**
	 * 使用@ModelAttribute, 实现Struts2 Preparable二次部分绑定的效果,先根据form的id从数据库查出Task对象,再把Form提交的内容绑定到该对象上。
	 * 因为仅update()方法的form中有id属性，因此本方法在该方法中执行.
	 */
	@ModelAttribute("preloadTask")
	public Objects getObjects(@RequestParam(value = "objectId", required = false) String objectId) {
		if (objectId != null) 
		{
			return objectsService.getObjects(objectId);
		}
		return null;
	}
}

